package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.MainActivity;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.address.EditAddressActivity;
import com.stn.zjx.givingtreasure.constants.StnConstants;
import com.stn.zjx.givingtreasure.domain.UserAddressDomain;
import com.stn.zjx.givingtreasure.utils.LanguageUtils;
import com.vondear.rxtools.RxActivityTool;
import com.vondear.rxtools.RxSPTool;

import java.util.ArrayList;
import java.util.List;

public class AddressManagerItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<UserAddressDomain.DataBean> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;

   public AddressManagerItemListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_collect_address, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       UserAddressDomain.DataBean    dataBean= dataBeanList.get(i);
                     ih.name.setText(dataBean.getUserAddressName());
                     ih.address.setText(dataBean.getUserAddressProvinceCityArea()+
                     " "+dataBean.getUserAddressAddress());
                  switch (dataBean.getAddressIsDefault()){
                      case 0:
                          ih.is_default.setVisibility(View.GONE);
                          break;
                      case 1:
                          ih.is_default.setVisibility(View.VISIBLE);
                          break;
                  }
                  if(TextUtils.isEmpty(dataBean.getUserAddressPhone())){
                      ih.phone.setText("");
                  }else{
                      if(dataBean.getUserAddressPhone().length()==11){
                          String start=dataBean.getUserAddressPhone().substring(0,3);
                          String end=dataBean.getUserAddressPhone().substring(dataBean.getUserAddressPhone().length()-4,dataBean.getUserAddressPhone().length());
                          ih.phone.setText(start+"****"+end);
                      }else{
                          ih.phone.setText(dataBean.getUserAddressPhone());
                      }

                  }
                  ih.tag.setText(dataBean.getUserAddressTag());
                  ih.right.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
                          Bundle bundle=new Bundle();
                          bundle.putSerializable("data",dataBean);
                          RxActivityTool.skipActivity(context, EditAddressActivity.class,bundle);
                      }
                  });
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<UserAddressDomain.DataBean> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<UserAddressDomain.DataBean> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
           public TextView name;
           public TextView tag;
           public TextView is_default;
           public TextView address;
           public RelativeLayout right;
           public TextView phone;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            phone=itemView.findViewById(R.id.phone);
            name=itemView.findViewById(R.id.name);
            tag=itemView.findViewById(R.id.tag);
            is_default=itemView.findViewById(R.id.is_default);
            address=itemView.findViewById(R.id.address);
            right=itemView.findViewById(R.id.right);
        }
   }

}